<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
      <el-form-item>
        <el-select v-model="dataForm.grade" placeholder="会员身份">
          <el-option v-for="item in options" :key="item.val" :label="item.label" :value="item.val"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.status" placeholder="审核状态">
          <el-option
            v-for="item in optionState"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.statisticMonthDate"
          type="month"
          value-format="yyyy-MM-dd"
          placeholder="选择月"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.nickname" placeholder="昵称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataForm.number" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button
          type="primary"
          :disabled="dataListSelections.length <= 0"
          @click="addOrUpdateHandle()"
        >批量审核</el-button>
        <el-button type="primary" @click="download()">下载</el-button>
      </el-form-item>
    </el-form>
    <div class="winNumber">
      <span>申请提现总金额：{{withdrawApplyBalance}}元</span>
      <span>打款成功金额总数：{{withdrawSuccessBalance}}元</span>
      <span>总税费：{{withdrawPayFee}}元</span>
    </div>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="userId" header-align="center" align="center" label="uid"></el-table-column>
      <el-table-column prop="nickname" header-align="center" align="center" label="昵称"></el-table-column>
      <el-table-column prop="coverUrl" header-align="center" align="center" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.avatar" style="width: 40px;height: 40px;" />
        </template>
      </el-table-column>
      <el-table-column prop="phone" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="wxNumber" header-align="center" align="center" label="微信号"></el-table-column>
      <el-table-column header-align="center" align="center" label="会员身份">
        <template slot-scope="scope">
          {{scope.row.grade==1 ?'会员':''}}
          {{scope.row.grade==2 ?'经理':''}}
          {{scope.row.grade==3 ?'总监':''}}
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="职位">
        <template slot-scope="scope">
          <span>{{ scope.row.title === 0 ? '班长(正常分销) ' : '' }}</span>
          <span>{{ scope.row.title === 1 ? '班主任' : '' }}</span>
          <span>{{ scope.row.title === 2 ? '班长(禁止分销)' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="realName" header-align="center" align="center" label="真实姓名"></el-table-column>
      <el-table-column prop="idCard" header-align="center" align="center" label="纳税身份证号"></el-table-column>
      <el-table-column
        prop="authenticatedPhone"
        header-align="center"
        align="center"
        label="实名认证手机号"
      ></el-table-column>
      <el-table-column prop="alipayAccount" header-align="center" align="center" label="支付宝帐号"></el-table-column>
      <el-table-column prop="bankCardNumber" header-align="center" align="center" label="银行卡号"></el-table-column>
      <el-table-column prop="bankName" header-align="center" align="center" label="开户行"></el-table-column>
      <el-table-column prop="withDrawOrderId" header-align="center" align="center" label="提现订单号"></el-table-column>
      <el-table-column prop="balance" header-align="center" align="center" label="提现金额"></el-table-column>
      <el-table-column prop="taxRatio" header-align="center" align="center" label="税率"></el-table-column>
      <el-table-column prop="payFee" header-align="center" align="center" label="扣税金额"></el-table-column>
      <el-table-column prop="realWithdrawBalance" header-align="center" align="center" label="实际到账金额"></el-table-column>
      <el-table-column prop="gmtCreate" header-align="center" align="center" label="创建时间"></el-table-column>
      <el-table-column header-align="center" align="center" label="状态">
        <template slot-scope="scope">
          {{scope.row.auditStatus===1 ?'待审核':''}}
          {{scope.row.auditStatus===2 ?'打款中':''}}
          {{scope.row.auditStatus===3 ?'审核驳回':''}}
          {{scope.row.auditStatus===4 ?'打款成功':''}}
          {{scope.row.auditStatus===5 ?'打款失败':''}}
        </template>
      </el-table-column>
      <el-table-column
        prop="paymentFailReason"
        header-align="center"
        align="center"
        label="打款失败原因"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="withdrawMethod" header-align="center" align="center" label="提现方式">
        <template slot-scope="scope">
          {{ scope.row.withdrawMethod || '——' }}
        </template>
      </el-table-column>
      <el-table-column prop="auditAdminName" header-align="center" align="center" label="最后审核人"></el-table-column>
      <el-table-column
        prop="auditRemarks"
        header-align="center"
        align="center"
        label="备注"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column prop="auditTime" header-align="center" align="center" label="最后审核时间"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.auditStatus===1||scope.row.auditStatus===5"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.id)"
          >审核并打款</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

    <with-draw-manage-reviewed ref="addOrUpdate" @refreshDataList="getDataList"></with-draw-manage-reviewed>
  </div>
</template>

<script>
import WithDrawManageReviewed from "./withDrawManage-reviewed";

export default {
  data() {
    return {
      withdrawApplyBalance: "",
      withdrawSuccessBalance: "",
      withdrawPayFee: "",
      options: [
        {
          val: null,
          label: "全部"
        },
        {
          val: "1",
          label: "普通会员"
        },
        {
          val: "2",
          label: "经理"
        },
        {
          val: "3",
          label: "总监"
        }
      ],
      optionState: [
        {
          val: null,
          label: "全部"
        },
        {
          val: "1",
          label: "待审核"
        },
        {
          val: "4",
          label: "打款成功"
        },
        {
          val: "2",
          label: "打款中"
        },
        {
          val: "3",
          label: "审核驳回"
        },
        {
          val: "5",
          label: "打款失败"
        }
      ],
      dataForm: {
        key: "",
        rember: "",
        uid: "",
        phone: "",
        nickname: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      // 上架
      shelfVisible: false,
      viewDetail: false,
      isAlive: "",
      bookmanages: true,
      bookchapters: false //章节
    };
  },
  components: {
    WithDrawManageReviewed
  },
  mounted() {
    this.getDataList();
  },
  methods: {
    search(){
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key
      };
      if (this.dataForm.grade) {
        obj.grade = this.dataForm.grade;
      }
      if (this.dataForm.status) {
        obj.status = this.dataForm.status;
      }
      if (this.dataForm.userId) {
        obj.userId = this.dataForm.userId;
      }
      if (this.dataForm.nickname) {
        obj.nickname = this.dataForm.nickname;
      }
      if (this.dataForm.statisticMonthDate) {
        obj.statisticMonthDate = this.dataForm.statisticMonthDate;
      }
      if (this.dataForm.number) {
        obj.number = this.dataForm.number;
      }
      this.$http({
        url: this.$http.adornUrl("/account/salewithdraw/list"),
        method: "get",
        params: this.$http.adornParams(obj)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
      let localMonthDate = "";
      if (this.dataForm.statisticMonthDate) {
        localMonthDate = this.dataForm.statisticMonthDate;
      } else {
        const time = new Date();
        localMonthDate = `${time.getFullYear()}-${
          time.getMonth() + 1 < 10
            ? "0" + (time.getMonth() + 1)
            : time.getMonth() + 1
        }-01`;
      }
      this.$http({
        url: this.$http.adornUrl(
          "/account/salewithdraw/getMonthStatisticByDate"
        ),
        method: "get",
        params: this.$http.adornParams({ localMonthDate: localMonthDate })
      }).then(({ data }) => {
        this.withdrawApplyBalance = data.totalStatistic.withdrawApplyBalance;
        this.withdrawPayFee = data.totalStatistic.withdrawPayFee;
        this.withdrawSuccessBalance =
          data.totalStatistic.withdrawSuccessBalance;
      });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      let ids = "";
      if (id) {
        ids = id.toString();
      } else {
        let newArr = [];
        this.dataListSelections.map(item => {
          newArr.push(item.id);
        });
        ids = newArr.join(",");
      }
      this.$nextTick(() => {
        this.$refs.addOrUpdate.getDataList(ids);
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val.filter(item => {
        return item.auditStatus === 1 || item.auditStatus === 5;
      });
      console.log(this.dataListSelections);
    },
    download() {
      let obj = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key
      };
      if (this.dataForm.grade) {
        obj.grade = this.dataForm.grade;
      }
      if (this.dataForm.status) {
        obj.status = this.dataForm.status;
      }
      if (this.dataForm.userId) {
        obj.userId = this.dataForm.userId;
      }
      if (this.dataForm.nickname) {
        obj.nickname = this.dataForm.nickname;
      }
      if (this.dataForm.statisticMonthDate) {
        obj.statisticMonthDate = this.dataForm.statisticMonthDate;
      }
      if (this.dataForm.number) {
        obj.number = this.dataForm.number;
      }
      this.$http({
        url: this.$http.adornUrl("/account/salewithdraw/download"),
        method: "get",
        params: this.$http.adornParams(obj),
        headers: {
          "Content-Type": "application/json; application/octet-stream"
        },
        responseType: "arraybuffer"
      }).then(({ data }) => {
        const blob = new Blob([data], { type: "application/vnd.ms-excel" });
        const downloadElement = document.createElement("a");
        const href = window.URL.createObjectURL(blob);
        downloadElement.href = href;
        downloadElement.download = "提现.xls";
        document.body.appendChild(downloadElement);
        downloadElement.click();
        document.body.removeChild(downloadElement); // 下载完成移除元素
        window.URL.revokeObjectURL(href); // 释放掉blob对象
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.winNumber {
  color: #777;
  margin-bottom: 20px;
  span {
    margin-right: 20px;
  }
}
</style>
